//
//  HWerweiMaFenXiangView.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/21.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWerweiMaFenXiangView.h"
@interface HWerweiMaFenXiangView()
@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) NSArray *listArr;

@property(strong,nonatomic) UIImageView *fenxiangImge;



@end

@implementation HWerweiMaFenXiangView
- (instancetype)initWithFrame:(CGRect)frame  {
    
    if (self  = [super initWithFrame:frame]) {
        // 添加灰色背景
        [self addgrayView];
        
        [self setupUI];
        
        
    }
    return  self;
    
}

//  设置UI
- (void)setupUI {
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-138-38-TabbarSafeBottomMargin, kScreenW, 138+38+TabbarSafeBottomMargin)];
    self.fenxiangView.backgroundColor = RGB(240, 244, 246);
    [self addSubview:self.fenxiangView];
    
    
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-120)/2, 10, 120, 120)];
    self.fenxiangImge = image1;
    [self.fenxiangView addSubview:image1];
    
    
    
    
    
    
//    UILabel *fenxianlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 16, kScreenW, 13)];
//    fenxianlabel.text = @"分享游戏";
//    fenxianlabel.font = [UIFont systemFontOfSize:13];
//    fenxianlabel.textColor = RGB(51, 51, 51);
//    fenxianlabel.textAlignment =  NSTextAlignmentCenter;
//    [self.fenxiangView  addSubview:fenxianlabel];
//    
//    self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
//    self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
//    CGFloat jianju = (kScreenW - 82-4*50)/3;
//    // CGFloat  shujianju = (166-100)/3;
//
//    for (int i = 0 ; i < self.listArr.count; i++) {
//      
//
//        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
//        butt.tag = i;
//        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
//        // butt.backgroundColor = [UIColor redColor];
//        [self.fenxiangView addSubview:butt];
//
//    }
    
    UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,137, kScreenW, 1)];
    fengeview.backgroundColor = RGB(215, 215, 215);
    
    
    [self.fenxiangView addSubview:fengeview];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 138, kScreenW, 38);
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitle:[HWjianzhiduiObject keylocalization:@"保存"] forState:UIControlStateNormal];
    [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.fenxiangView addSubview:button];
    
    
    
    
    
    
    
    
    
    
}


- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//

    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


//点击buton 按钮

- (void)buttonTap:(UIButton *)sender {
    
 //   [self.delegate xuazhongTag:(int)sender.tag];
    
    
    
   
    
}

#pragma mark ------ 取消
- (void)quiabuttontap {
    
  //  [self removeFromSuperview];
    
    
   UIImageWriteToSavedPhotosAlbum(self.fenxiangImge.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    #pragma mark -- <保存到相册>
   
    
    
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
        [MBProgressHUD showSuccess:@"保存失败"];
        
    }else{
        msg = @"保存图片成功" ;
           [MBProgressHUD showSuccess:@"保存成功"];
    }
}



//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}


- (void)setUrlStr:(NSString *)urlStr {
    
    _urlStr = urlStr;
    
    self.fenxiangImge.image =[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:urlStr] withSize:150];

    
    
}

- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
    
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}



@end
